package com.sykj.bsc.rwl.service.Impl;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sykj.bsc.rwl.mapper.SysUserMapper;
import com.sykj.bsc.rwl.po.SysMenu;
import com.sykj.bsc.rwl.po.SysRole;
import com.sykj.bsc.rwl.po.SysUser;
import com.sykj.bsc.rwl.po.extend.SysMenuExtend;
import com.sykj.bsc.rwl.po.extend.SysUserExtend;
import com.sykj.bsc.rwl.service.SysUserService;
import com.sykj.bsc.rwl.util.JedisClientPool;
import com.sykj.bsc.rwl.util.JsonUtils;
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService{

	@Autowired
	private SysUserMapper sysUserMapper;
	/*@Autowired
	private JedisClientPool pool;*/
	
	//通过用户账号查询用户信息
	public SysUser findByIdSysUser(String account) throws Exception {
		//String json=pool.get("Sysuser"+account+"q1");
		SysUser sysUser=sysUserMapper.findByIdSysUser(account);
		/*if(json==null) {
			sysUser=sysUserMapper.findByIdSysUser(account);
			json=JsonUtils.objectToJson(sysUser);
			pool.set("Sysuser"+account+"q1", json);
		}else {
			sysUser=JsonUtils.jsonToPojo(json, SysUser.class);
		}*/
		return sysUser;
	}

	//通过用户id查询角色
	public SysRole findByIdSysRole(Integer user_id) throws Exception {
		//String json=pool.get("SysUserrole"+user_id+"q2");
		SysRole sysRole=sysUserMapper.findByIdSysRole(user_id);
		/*if(json==null) {
			sysRole=sysUserMapper.findByIdSysRole(user_id);
			json=JsonUtils.objectToJson(sysRole);
			pool.set("SysUserrole"+user_id+"q2", json);
		}else {
			sysRole=JsonUtils.jsonToPojo(json, SysRole.class);
		}*/
		return sysRole;
	}

	//通过角色id查询权限
	public List<SysMenuExtend> findByIdSysMenu(Integer role_id) throws Exception {
		//String json=pool.get("SysUserSysMenuExtend"+role_id+"q3");
		List<SysMenuExtend> list=sysUserMapper.findByIdSysMenu(role_id);
		/*if(json==null) {
			list= sysUserMapper.findByIdSysMenu(role_id);
			json=JsonUtils.objectToJson(list);
			pool.set("SysUserSysMenuExtend"+role_id+"q3", json);
		}else {
			list=JsonUtils.jsonToList(json, SysMenuExtend.class);
		}*/
		return list;
	}

	@Override
	public List<SysMenu> findByIdButton(HashMap<String, Integer> map) throws Exception {
		//String json=pool.get("SysUserSysMenuMapq4");
		List<SysMenu> list=sysUserMapper.findByIdButton(map);
		/*if(json==null) {
			list=sysUserMapper.findByIdButton(map);
			json=JsonUtils.objectToJson(list);
			pool.set("SysUserSysMenuMapq4", json);
		}else {
			list=JsonUtils.jsonToList(json, SysMenu.class);
		}*/
		return list;
	}

	@Override
	public List<SysUserExtend> selectSysUser(SysUserExtend sysUserExtend) throws Exception {
		//String json=pool.get("SysUserExtendq5");
		List<SysUserExtend> list=sysUserMapper.selectSysUser(sysUserExtend);
		/*if(json==null) {
			list=sysUserMapper.selectSysUser(sysUserExtend);
			json=JsonUtils.objectToJson(list);
			pool.set("SysUserExtendq5", json);
		}else {
			if(sysUserExtend!=null) {
				list=sysUserMapper.selectSysUser(sysUserExtend);
				json=JsonUtils.objectToJson(list);
				pool.set("SysUserExtendq5", json);
			}
			list=JsonUtils.jsonToList(json, SysUserExtend.class);
		}*/
		return list;
	}

	@Override
	public void insertSysUser(SysUserExtend sysUserExtend,int user_id) throws Exception {
		if(sysUserExtend!=null) {
			Object result=Md5Encrypt(sysUserExtend);
			String password=result.toString();
			sysUserExtend.setPassword(password);//存储当前密码
		}
		sysUserExtend.setCreate_user_id(String.valueOf(user_id));
		sysUserMapper.insertSysUser(sysUserExtend);
		int user_id2=sysUserExtend.getUser_id();
		sysUserExtend.setUser_id(user_id2);
		sysUserMapper.insertSysUserRole(sysUserExtend);
	}
	
	/**
	 * 
	 * @Title: Md5Encrypt   
	 * @Description: Md5密码加密 
	 * @param: @param sysUserExtend
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public Object Md5Encrypt(SysUserExtend sysUserExtend) {
		String algorithmName="Md5";//散列名称
		String source=sysUserExtend.getPassword();//未加密之前的密码
		ByteSource salt=ByteSource.Util.bytes(sysUserExtend.getAccount());//通过用户名加盐
		int hashIterations=1024;//散列次数
		Object result=new SimpleHash(algorithmName, source, salt, hashIterations);
		return result;
	}

	@Override
	public void updateSysUser(SysUserExtend sysUserExtend, int user_id) throws Exception {
		sysUserExtend.setModify_user_id(String.valueOf(user_id));
		sysUserMapper.updateSysUser(sysUserExtend);
		sysUserMapper.updateSysUserRole(sysUserExtend);
	}

	@Override
	public SysUserExtend findByIdSelectSysUser(Integer user_id) throws Exception {
		//String json=pool.get("SysUserExtend"+user_id+"q6");
		SysUserExtend sysUserExtend=sysUserMapper.findByIdSelectSysUser(user_id);
		/*if(json==null) {
			sysUserExtend=sysUserMapper.findByIdSelectSysUser(user_id);
			json=JsonUtils.objectToJson(sysUserExtend);
			pool.set("SysUserExtend"+user_id+"q6",json);
		}else {
			sysUserExtend=JsonUtils.jsonToPojo(json, SysUserExtend.class);
		}*/
		return sysUserExtend;
	}

	@Override
	public void deleteSysUser(Integer user_id) throws Exception {
		sysUserMapper.deleteSysUser(user_id);
	}

	@Override
	public void unfreezeSysUser(HashMap<String, Object> map) throws Exception {
		sysUserMapper.unfreezeSysUser(map);
	}
}
